package com.ncloud.chnl.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSONArray;
import com.sun.media.jfxmedia.logging.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.ncloud.chnl.common.Constant;
import com.ncloud.chnl.entity.CustUser;
import com.ncloud.chnl.mapper.CustUserMapper;
import com.ncloud.chnl.service.CustUserService;
import com.ncloud.common.enmus.ResultEnum;
import com.ncloud.common.exception.UserException;
import com.ncloud.common.utils.PageHelperUtils;
import com.ncloud.param.service.SequenceService;

@Service
public class CustUserServiceImpl implements CustUserService {
	
	@Reference
	private SequenceService sequence;
	@Autowired
	private CustUserMapper mapper;
	
	@Override
	public Object createCustUserRel(JSONObject json) {
		// 校验参数
		System.out.println(json.toJSONString());
		if(!json.containsKey("custId")){
			throw new UserException(ResultEnum.PARAM_ERROR);
		}
		// 组织参数
		Long userId = json.getLong("userID");
		Long relInstId = sequence.getSequence("REL_INST_ID_SEQ");
		CustUser custUser = JSON.parseObject(json.toJSONString(), CustUser.class);
		custUser.setCreateUser(userId);
		custUser.setUpdateUser(userId);
		custUser.setCreateTime(new Date());
		custUser.setUpdateTime(new Date());
		custUser.setRelInstId(relInstId);
		custUser.setState(Constant.CUST_AREA_NODE_STATE_NORMAL);
		// 插入数据库
		try {
			mapper.insertCustUser(custUser);
		} catch (Exception e) {
			throw new UserException(ResultEnum.DATABASE_ERROR+e.toString());
		}
		return HttpStatus.CREATED;
	}

	@Override
	public Object deleteCustUserRel(JSONObject json) {
		if(!json.containsKey("relInstId")){
			throw new UserException(ResultEnum.PARAM_ERROR);
		}
		Long relInstId = json.getLong("relInstId");
		Long userId = json.getLong("userID");
		CustUser custUser = new CustUser();
		custUser.setRelInstId(relInstId);
		custUser.setState(Constant.CUST_AREA_NODE_STATE_DELETE);
		custUser.setUpdateTime(new Date());
		custUser.setUpdateUser(userId);
		try {
			mapper.updateCustUser(custUser);
		} catch (Exception e) {
			throw new UserException(ResultEnum.DATABASE_ERROR+e.toString());
		}
		return HttpStatus.OK;
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	@Override
	public Object getCustUserList(JSONObject json) {
//		if(!json.containsKey("custId")){
//			throw new UserException(ResultEnum.PARAM_ERROR);
//		}
		Long custId = getCustByUserId(json);
		PageInfo pageInfo =null;
		Object object = null;
		if(PageHelperUtils.isPage(json)){
			PageHelperUtils.setPageParam(json);
			List<Map<String,Object>> custUsers = mapper.getCustUsers(custId);
			pageInfo = new PageInfo(custUsers);
			object = JSON.parseObject(JSON.toJSONString(pageInfo));
		}else {
			List<Map<String,Object>> custUsers = mapper.getCustUsers(custId);
			object = JSON.parseArray(JSON.toJSONString(custUsers));
		}
		
		return object;
	}
	
	public Long getCustByUserId(JSONObject json){
		Long userId = null;
		if(json.containsKey("userId")){
			userId = json.getLong("userId");
		}else{
			userId = json.getLong("userID");
		}
		CustUser custUser = mapper.getCustByUserId(userId);
		Long custId = null;
		if(custUser!=null){
			custId = custUser.getCustId();
		}
			
		return custId;
	}
	
	@SuppressWarnings({ "rawtypes", "unchecked" })
	@Override
	public Object getAllCustUserList(JSONObject json) {
		PageInfo pageInfo =null;
		Object object = null;
		if(PageHelperUtils.isPage(json)){
			PageHelperUtils.setPageParam(json);
			List<Map<String, Object>> custUsers = mapper.getAllCustUsers();
			pageInfo = new PageInfo(custUsers);
			object = JSON.parseObject(JSON.toJSONString(pageInfo));
		}else {
			List<Map<String, Object>> custUsers = mapper.getAllCustUsers();
			object = JSON.parseArray(JSON.toJSONString(custUsers));
		}
		
		return object;
	}

	@Override
	public JSONArray getUserIdByCustId(Long custId) {
		List<CustUser> list = mapper.getUserIdByCustId(custId);
		return JSON.parseArray(JSON.toJSONString(list));
	}
}
